﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace AcademicAudit_JoelShuman
{
	public class XMLio : StudentFileIO
	{
		public string GetStudentDirPath()
		{
			return "../../DataFiles/Students/";
		}

		public MainRecords ReadStudentFiles()
		{
			MainRecords theRecords = new MainRecords();
			var dirinfo = new DirectoryInfo(MainRecords.STUDENTDIR);
			var theFiles = dirinfo.GetFiles("*.xml", SearchOption.TopDirectoryOnly).ToList();

			foreach (var file in theFiles)
			{
				StreamReader tempS = null;
				FileStream tempFs = null;
				try
				{
					XmlSerializer x = new XmlSerializer(typeof(StudentRecord));
					tempFs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
					tempS = new StreamReader(tempFs);
					theRecords.m_AllStudents.Add((StudentRecord)x.Deserialize(tempS));
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex);
					theRecords = null;
				}
				finally
				{
					if (tempS != null) tempS.Close();
					if (tempFs != null) tempFs.Close();
				}
			}
			return theRecords;
		}

		public void ReadDefaultCourseFile()
		{
			StreamReader tempS = null;
			FileStream tempFs = null;
			try
			{
				// Read in the default course data into memory.  Used when a new student is created.
				XmlSerializer x = new XmlSerializer(typeof(List<Course>));
				tempFs = new FileStream("../../DataFiles/DefaultCourse.xml", FileMode.Open, FileAccess.Read);
				tempS = new StreamReader(tempFs);
				DefaultCourseList.DEFAULTCOURSELIST = (List<Course>)x.Deserialize(tempS);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
			}
			finally
			{
				if (tempS != null) tempS.Close();
				if (tempFs != null) tempFs.Close();
			}
		}

		public void SaveToDisk(MainRecords theRecords)
		{
			foreach (StudentRecord thisStudent in theRecords.m_AllStudents)
			{
				FileStream tempFs = null;
				StreamWriter tempS = null;
				try
				{
					XmlSerializer x = new XmlSerializer(typeof(StudentRecord));
					tempFs = new FileStream(MainRecords.STUDENTDIR + thisStudent.m_FileName + ".xml", FileMode.Truncate, FileAccess.Write);
					tempS = new StreamWriter(tempFs);
					x.Serialize(tempS, thisStudent);
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex);
				}
				finally
				{
					if (tempS != null) tempS.Close();
					if (tempFs != null) tempFs.Close();
				}
			}
		}

		public bool AddStudent(StudentRecord newStudent)
		{
			// Write an XML file for the student.
			FileStream tempFs = null;
			StreamWriter tempS = null;
			try
			{
				XmlSerializer x = new XmlSerializer(typeof(StudentRecord));
				tempFs = new FileStream(MainRecords.STUDENTDIR + newStudent.m_FileName + ".xml", FileMode.OpenOrCreate, FileAccess.Write);
				tempS = new StreamWriter(tempFs);
				x.Serialize(tempS, newStudent);

				if (!StudentIDNumbers.VerifyID(newStudent.m_StudentID))
				{
					MessageBox.Show(newStudent.m_StudentID + " is not a valid Student ID!\n\nUnable to create the student!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
					// Delete the file.
					File.Delete(MainRecords.STUDENTDIR + newStudent.m_FileName + ".xml");
					StudentIDNumbers.FixIDFile();
					return false;
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
				return false;
			}
			finally
			{
				if (tempS != null) tempS.Close();
				if (tempFs != null) tempFs.Close();
			}
			return true;
		}

        //public bool VerifyID(string theID)
        //{
        //    throw new NotImplementedException();
        //}

        //public void GetAllActiveIDs()
        //{
        //    throw new NotImplementedException();
        //}
	}
}
